package com.zhou.yygh.hosp.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.zhou.yygh.hosp.repository.DepartmentRepository;
import com.zhou.yygh.hosp.service.DepartmentService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.stereotype.Service;
import zhou.hospital.model.hosp.Department;
import zhou.hospital.vo.hosp.DepartmentQueryVo;
import zhou.hospital.vo.hosp.DepartmentVo;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class DepartmentServiceImpl implements DepartmentService {
    @Autowired
    private DepartmentRepository departmentRepository;

    //删除部门
    @Override
    public void deleteDept(String hoscode, String depcode) {
        Department department = departmentRepository.getDepartmentByHoscodeAndDepcode(hoscode, depcode);
        if (department != null) {
            departmentRepository.deleteById(department.getId());
        }
    }

    //分页查询部门
    @Override
    public Page<Department> findPageDepartment(Integer page, Integer limit, DepartmentQueryVo departmentQueryVo) {

        Department department = new Department();
        BeanUtils.copyProperties(departmentQueryVo, department);
        department.setIsDeleted(0);

        Pageable pageable = PageRequest.of(page - 1, limit);

        ExampleMatcher exampleMatcher = ExampleMatcher.matching()
                .withStringMatcher(ExampleMatcher.StringMatcher.CONTAINING)
                .withIgnoreCase(true);
        Example<Department> example = Example.of(department, exampleMatcher);

        Page<Department> all = departmentRepository.findAll(example, pageable);
        return all;
    }


    //增加部门
    @Override
    public void save(Map<String, Object> paramMap) {
        String mapString = JSONObject.toJSONString(paramMap);
        Department department = JSONObject.parseObject(mapString, Department.class);
        //根据医院编号和部门查询
        Department departmentExit = departmentRepository.getDepartmentByHoscodeAndDepcode(department.getHoscode(), department.getDepcode());
        if (departmentExit != null) {
            department.setUpdateTime(new Date());
            department.setIsDeleted(0);
            departmentRepository.save(department);
        } else {
            department.setCreateTime(new Date());
            department.setUpdateTime(new Date());
            department.setIsDeleted(0);
            departmentRepository.save(department);
        }


    }


    //得到部门排序
    @Override
    public List<DepartmentVo> getDeptTree(String hoscode) {
        ArrayList<DepartmentVo> list = new ArrayList<>();
        //根据hoscode得到所有部门列表
        Department queryVo = new Department();
        queryVo.setHoscode(hoscode);
        Example<Department> example = Example.of(queryVo);
        List<Department> departmentList = departmentRepository.findAll(example);

        //根据Bigcode 大科室分组，获取大科室下所有小科室
        Map<String, List<Department>> listMap = departmentList.stream().collect(Collectors.groupingBy(Department::getBigcode));

        for (Map.Entry<String, List<Department>> entry : listMap.entrySet()) {
            //大科室
            String bigCode = entry.getKey();
            //大科室下对应的所有小科室数据
            List<Department> departmentList1 = entry.getValue();

            //封装大科室
            DepartmentVo departmentVo = new DepartmentVo();
            departmentVo.setDepcode(bigCode);
            departmentVo.setDepname(departmentList1.get(0).getBigname());

            //封装小科室
            List<DepartmentVo> departmentVoList = new ArrayList<>();
            for (Department department : departmentList1) {
                DepartmentVo departmentVo1 = new DepartmentVo();
                departmentVo1.setDepname(department.getDepname());
                departmentVo1.setDepcode(department.getDepcode());
                departmentVoList.add(departmentVo1);
            }
            //把小科室放入大科室中
            departmentVo.setChildren(departmentVoList);

            //放入最终传输集合中
            list.add(departmentVo);

        }
        return list;
    }

    @Override
    public String getDepName(String hoscode,String depcode) {
       Department department= departmentRepository.getDepartmentByHoscodeAndDepcode(hoscode,depcode);
       if(department!=null){
           return department.getDepname();
       }
       return null;

    }

    @Override
    public Department getDepartment(String hoscode, String depcode) {
       return departmentRepository.getDepartmentByHoscodeAndDepcode(hoscode,depcode);
    }
}
